package com.liujit.standard.basis.core.filter;

import com.liujit.standard.basis.core.util.XssUtil;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * @description: XSS过滤类
 * @author: liujun
 * @create: 2021/3/28 5:19 下午
 **/
public class XssRequestWrapper extends HttpServletRequestWrapper {

    public XssRequestWrapper(HttpServletRequest request) {
        super(request);
    }


    @Override
    public String getHeader(String name) {
        return super.getHeader(name);
    }

    @Override
    public String getQueryString() {
        return XssUtil.clearXss(super.getQueryString());
    }

    @Override
    public String getParameter(String name) {
        return XssUtil.clearXss(super.getParameter(name));
    }

    @Override
    public String[] getParameterValues(String name) {
        String[] values = super.getParameterValues(name);
        if(values != null) {
            int length = values.length;
            String[] ss = new String[length];
            for(int i = 0; i < length; i++){
                ss[i] = XssUtil.clearXss(values[i]);
            }
            return ss;
        }
        return super.getParameterValues(name);
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        Map<String, String[]> parameterMap = super.getParameterMap();
        if (parameterMap != null) {
            for (String k : parameterMap.keySet()) {
                String[] values = parameterMap.get(k);
                if(values != null) {
                    int length = values.length;
                    for(int i = 0; i < length; i++){
                        values[i] = XssUtil.clearXss(values[i]);
                    }
                }
            }
        }
        return super.getParameterMap();
    }
}
